﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using SmartGrid.NET.WPFUtils.Interfaces;

namespace SmartGrid.NET.WPFUtils.Converters
{
    public class EnumConverterBase<E, T> :
        ConverterBase<T>,
        IEnumConverter<E>
        where E : struct
        where T : class, IEnumConverter<E>, new()
    {
        #region State

        protected static Dictionary<E, string> _MapEnum2String = new Dictionary<E, string>();

        protected static Dictionary<string, E> _MapString2Enum = new Dictionary<string, E>();

        #endregion

        #region CtorDtor
        static EnumConverterBase()
        {
            var t = typeof(E);

            foreach (E e in Enum.GetValues(t))
            {
                _MapEnum2String[e] = Enum.GetName(t, e);

                _MapString2Enum[_MapEnum2String[e]] = e;
            }
        }
        #endregion

        #region IEnumConverter

        protected virtual Dictionary<E, string> MapEnum2String
        {
            get { return _MapEnum2String; }
        }
        Dictionary<E, string> IEnumConverter<E>.MapEnum2String
        {
            get { return MapEnum2String; }
        }

        protected virtual Dictionary<string, E> MapString2Enum
        {
            get { return _MapString2Enum; }
        }
        Dictionary<string, E> IEnumConverter<E>.MapString2Enum
        {
            get { return MapString2Enum; }
        }

        protected virtual string[] Convert(E[] enums)
        {
            throw new NotImplementedException();
        }
        string[] IEnumConverter<E>.Convert(E[] enums)
        {
            return Convert(enums);
        }

        protected virtual string Convert(E vl)
        {
            throw new NotImplementedException();
        }
        string IEnumConverter<E>.Convert(E vl)
        {
            return Convert(vl);
        }

        protected virtual string Convert()
        {
            throw new NotImplementedException();
        }
        string IEnumConverter<E>.Convert()
        {
            return Convert();
        }

        protected virtual E? Convert(string vl)
        {
            throw new NotImplementedException();
        }
        E? IEnumConverter<E>.Convert(string vl)
        {
            return Convert(vl);
        }

        #endregion

        #region IValueConverter

        protected override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return base.Convert(value, targetType, parameter, culture);
        }

        protected override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object vl = null;

            if (typeof(string) == value.GetType())
            {
                vl = Convert((string)value);
            }

            return vl;
        }

        #endregion
    }
}
